/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmLaporanJurusan.java
 *
 * Created on 27 Apr 11, 14:12:33
 */
package toesp.view.jurusan;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.view.JasperViewer;
import toesp.bean.JurusanBean;
import toesp.entity.Jurusan;
import toesp.util.joptionPaneUtil;

/**
 *
 * @author satria.sp
 */
public class frmLaporanJurusan extends javax.swing.JDialog {
    JurusanBean DsJur = new JurusanBean();
    joptionPaneUtil msgbox = new joptionPaneUtil(); 
    /** Creates new form frmLaporanJurusan */
    public frmLaporanJurusan(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setCombo();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lblTitile = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        cmbJur = new javax.swing.JComboBox();
        radioJur = new javax.swing.JRadioButton();
        jPanel3 = new javax.swing.JPanel();
        btnCancel = new javax.swing.JButton();
        btnOK = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        lblTitile.setFont(new java.awt.Font("Times New Roman", 1, 12)); // NOI18N
        lblTitile.setText("TITLE FORM");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lblTitile, javax.swing.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lblTitile, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        radioJur.setSelected(true);
        radioJur.setText("Jurusan");
        radioJur.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                radioJurItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(radioJur)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cmbJur, 0, 299, Short.MAX_VALUE)
                .addGap(16, 16, 16))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(cmbJur, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(radioJur))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/toesp/image/Cancel.png"))); // NOI18N
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        btnOK.setIcon(new javax.swing.ImageIcon(getClass().getResource("/toesp/image/OK.png"))); // NOI18N
        btnOK.setText("OK");
        btnOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(216, Short.MAX_VALUE)
                .addComponent(btnOK)
                .addGap(16, 16, 16)
                .addComponent(btnCancel))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(btnCancel)
                .addComponent(btnOK))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void radioJurItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_radioJurItemStateChanged
        // TODO add your handling code here:
         
}//GEN-LAST:event_radioJurItemStateChanged

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
}//GEN-LAST:event_btnCancelActionPerformed

    private void setCombo(){
        try {
            List<Jurusan> lstJur = DsJur.getAll();
            cmbJur.addItem("--ALL--");
            for (Jurusan jurusan : lstJur) {
                cmbJur.addItem(jurusan.getJurusanNama());
            }
        } catch (Exception e) {
        }
    }
        
    @SuppressWarnings("unchecked")
    private List getDataAll(){
        List lstData = new ArrayList(); 
        
        try {
            List<Jurusan> lstJur = DsJur.getAll();
            for (Jurusan jur : lstJur) {
                lstData.add(jur);
            }
        } catch (Exception e) {
        }
        
        return lstData;
    }
    
    @SuppressWarnings("unchecked")
    private List getDataById(String pstrJurId){
        List lstData = new ArrayList(); 
        
        try {
            Jurusan jur = DsJur.getOnebyId(pstrJurId);             
            lstData.add(jur);
        } catch (Exception e) {
        }
        
        return lstData;
    }
    
    private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed
        // TODO add your handling code here:
        String currentDir = System.getProperty("user.dir");
        String fileName = currentDir +"/src/toesp/reporting/rptJurusan.jrxml";
        JRBeanCollectionDataSource dataSource;
        try {
            Map prs = new HashMap();
            JasperReport JRpt = JasperCompileManager.compileReport(fileName);
            if(cmbJur.getSelectedIndex()==0){
                dataSource = new JRBeanCollectionDataSource(getDataAll());
            }else{
                String jurNama = (String) cmbJur.getItemAt(cmbJur.getSelectedIndex());
                Jurusan j = DsJur.getOnebyName(jurNama);
                dataSource = new JRBeanCollectionDataSource(getDataById(j.getJurusanId()));
            }
            
            JasperPrint JPrint = JasperFillManager.fillReport(JRpt, null, dataSource);
            JasperViewer.viewReport(JPrint,false);
        } catch (Exception rptexcpt) {
            System.out.println(rptexcpt.getMessage());
        } 
    }//GEN-LAST:event_btnOKActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                frmLaporanJurusan dialog = new frmLaporanJurusan(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnOK;
    private static javax.swing.JComboBox cmbJur;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    public javax.swing.JLabel lblTitile;
    private javax.swing.JRadioButton radioJur;
    // End of variables declaration//GEN-END:variables
}
